<?php


namespace App\Http\Controllers\Admin;


use App\DTOS\Admin\BossDTO;
use App\Http\Controllers\Controller;

use App\Models\Game\Boss;
use App\Services\Game\BossService;
use App\Utils\ResponseUtil;
use Maatwebsite\Excel\Facades\Excel;

class BossController extends Controller
{
    private BossService $BossService;

    /**
     * SubjectController constructor.
     * @param BossService $BossService
     */
    public function __construct(BossService $BossService)
    {
        $this->BossService = $BossService;
    }

    public function import(){

        $data = request()->all();
        $name = data_get($data,'name');
        $file = request()->file('file');
        if ($file->getClientOriginalName() !== $name) {
            return ResponseUtil::error("请上传: " . $name);
        }

        $sheets = Excel::toArray(null,$file);
        $sheet = $sheets[0];
        Boss::query()->truncate();
        $inserts = [];
        foreach($sheet as $key=>$arr) {
            if ($key>1){
                $row = [
                    'name'=>$arr[1], //名称
                    'map'=>$arr[2], //出现地图
                    'level'=>$arr[3], //等级
                    'blood'=>$arr[4], //生命值
                    'attack'=>$arr[5], //攻击
                    'hit_rate'=>$arr[6], //命中率
                    'dodge'=>$arr[7], //闪避
                    'wk'=>$arr[8], //物抗
                    'fk_ice'=>$arr[9], //法抗,冰
                    'fk_fire'=>$arr[10], //法抗,火
                    'fk_electricity'=>$arr[11], //法抗,电
                    'fk_noxious'=>$arr[12], //法抗,毒
                    'drop_item'=>$arr[13], //掉落物品
                    'description'=>$arr[14], //描述
                ];
                $inserts[] = $row;
            }
        }
        Boss::query()->insert($inserts);
        return ResponseUtil::success();

    }

    public function paginate(){
        $paginate = $this->BossService->paginate(new BossDTO(request()->all()));
        $data = $paginate->items();
        $res = [];
        $res['code'] = 0;
        $res['count'] = $paginate->total();
        $res['data'] = $data;
        return ResponseUtil::json($res);
    }

    public function detail($id)
    {
        $detail = Boss::query()->find($id);
        return ResponseUtil::success($detail);
    }

    public function edit($id)
    {
        $detail = Boss::query()->find($id);

        $detail->updated_at = date("Y-m-d H:i:s");
        $detail->head_img = request()->get('head_img');
        $detail->body_img = request()->get('body_img');
        $detail->save();
        return ResponseUtil::success();
    }
}
